﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Gizmox.WebGUI.Forms;

namespace EstateNuke.BusinessSystem
{
    public class Utility
    {
        public static byte[] ToBytes(Stream stream)
        {
            var bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }
    }

    public class ComboBoxItem
    {
        public string Text { get; set; }
        public object Value { get; set; }
        public override string ToString()
        {
            return this.Text;
        }
    }
    public class DataResourceHandle : Gizmox.WebGUI.Common.Resources.GatewayResourceHandle
    {
        public DataResourceHandle(byte[] imageData, string contentType)
            : base(new Gizmox.WebGUI.Common.Gateways.GatewayReference(new DataComponent(imageData, contentType), "SendData"))
        {
        }

        private class DataComponent : Gizmox.WebGUI.Forms.Component
        {
            private byte[] data;
            private string contentType;

            public DataComponent(byte[] data, string contentType)
            {
                this.data = data;
                this.contentType = contentType;
                this.RegisterSelf();
            }

            public DataComponent(System.IO.Stream stream, string contentType)
            {
                this.data = GetStreamAsByteArray(stream);
                this.contentType = contentType;
                this.RegisterSelf();
            }

            protected override Gizmox.WebGUI.Common.Interfaces.IGatewayHandler ProcessGatewayRequest(HttpContext objHttpContext, string strAction)
            {
                if (strAction == "SendData")
                {
                    // Set ContentType
                    if (contentType != null)
                        objHttpContext.Response.ContentType = contentType;
                    // Disable Caching
                    objHttpContext.Response.Expires = -1;
                    // Send Data to Browser         
                    objHttpContext.Response.BinaryWrite(data);

                    objHttpContext.Response.Flush();
                    objHttpContext.ApplicationInstance.CompleteRequest();

                    // Clean up
                    data = null;
                }
                return null;
            }

            private byte[] GetStreamAsByteArray(System.IO.Stream stream)
            {
                int streamLength = Convert.ToInt32(stream.Length);

                byte[] fileData = new Byte[streamLength];

                // Read the Stream into a Byte array
                stream.Position = 0;
                stream.Read(fileData, 0, streamLength);
                stream.Close();

                return fileData;
            }

            protected override void Dispose(bool disposing)
            {
                // Remove Myself from the Current VWG Application
                this.UnRegisterSelf();

                base.Dispose(disposing);
            }
        }
    }
    public class JpegResourceHandle : DataResourceHandle
    {
        public JpegResourceHandle(byte[] jpegData) : base(jpegData, "image/jpeg") { }
    }
}